<?php

namespace app\common\model\system;
use app\common\model\BaseModel;
use app\common\repositories\system\CategoryRepository;

class Category extends BaseModel
{

    public static function tablePK(): ?string
    {
        return 'category_id';
    }

    public static function tableName(): string
    {
        return 'category';
    }

    /**
     * 获取父级名称
     * @param $value
     * @return string
     */
    public function getAncestorsAttr($value)
    {
        $value = self::whereIn('category_id',$this->path_ids)->order('level ASC')->column('cate_name');
        return implode('/',$value).'/'.$this->cate_name;
    }

    /**
     * 获取path的id
     * @return array
     */
    public function getPathIdsAttr()
    {
        return explode('/',$this->path);
    }

    /**
     * 获取子集id
     * @Author:Qinii
     * @Date: 2020/5/22
     * @param $value
     * @return array
     */
    public function getChildIdsAttr($value)
    {
        return self::where('path','like','%/'.$this->category_id.'/%')->column('category_id');
    }

    public function searchIdsAttr($query,$value)
    {
        $query->where('category_id','in',$value);
    }

    public function getTypeNameAttr()
    {
        return app()->make(CategoryRepository::class)::TYPE_MAP[$this->getData('type')]??"";
    }

}